package order

import (
	"context"
	"fmt"

	"go-weixin/app/order/api/internal/svc"
	"go-weixin/app/order/api/internal/types"

	"github.com/smartwalle/alipay/v3"
	"github.com/smartwalle/xid"
	"github.com/zeromicro/go-zero/core/logx"
)

type OrderPayReqLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderPayReqLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPayReqLogic {
	return &OrderPayReqLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderPayReqLogic) OrderPayReq(req *types.OrderPayReq) (resp *types.OrderPayResp, err error) {
	var tradeNo = fmt.Sprintf("%d", xid.Next())
	kServerDomain := ""
	var p = alipay.TradePagePay{}
	p.NotifyURL = kServerDomain + "/alipay/notify"
	p.ReturnURL = kServerDomain + "/alipay/callback"
	p.Subject = "支付测试:" + tradeNo
	p.OutTradeNo = tradeNo
	p.TotalAmount = "10.00"
	p.ProductCode = "FAST_INSTANT_TRADE_PAY"

	url, _ := l.svcCtx.AlipayClient.TradePagePay(p)
	resp = new(types.OrderPayResp)
	resp.Url = url.String()
	return
}
